import React, { Component } from 'react'
import { Pagination , Carousel } from 'antd';
import Header from '../../components/Header/header'
import Footer from '../../components/Footer/footer' 
import { getApiHonorBanner , getApiHonerInfo } from '../../axios/api'; 
import "./honor.scss"
import { get } from '../../axios/localstorage';

export default class honor extends Component {

    state = {
        banner:[],
        currentIndex:0,
        page:1,
        total:1,
        list:[],
    }

    componentWillMount(){
        getApiHonorBanner({
            city:get('city'),
            type:get('type')?get('type'):0,
            content:get('city')?get('city'):get('mechanism'),
        })
        .then( req =>{
            if(req.data.code === 1){
                this.setState({
                    banner:req.data.data
                })
            }
        }).catch( err =>{
            console.error(err);
        })

        this.getData();
    }

    onChange=(index)=>{
        this.setState({
            currentIndex:index
        })
    }

    changePage=(page)=>{
        this.setState({
            page
        })

        setTimeout(()=>{
            this.getData();
        },40)
    }

    gotoPage=(page)=>{
        this.props.history.push(page);
    }

    getData=()=>{
        let param = {
            page:this.state.page,
            city:get('city'),
            type:get('type')?get('type'):0,
            content:get('city')?get('city'):get('mechanism'),
        }

        getApiHonerInfo(param)
        .then( req =>{
            if(req.data.code === 1){
                let data = req.data.data;
                this.setState({
                    total:data.count,
                    list:data.list
                })
            }
        }).catch( err =>{
            console.error(err);
        })
    }

    render() {
        return (
            <div className='honor'>
                <Header />
                <div className='bannerBox'>
                    <div className='banner'>
                        <Carousel ref="bslide" dots={false} draggable={true} afterChange={(index)=>{this.onChange(index)}} autoplay={true} autoplaySpeed={3000}>
                            {
                                this.state.banner.map((item,index)=>{
                                    return <div className='item' key={index}>
                                                <div className="bg" style={{backgroundImage:"url('"+item.image+"')"}}></div>
                                            </div>
                                })
                            }
                        </Carousel>
                    </div>
                </div>
                <div className='page'>
                    <div className='pm'>
                        <div className="none" hidden={this.state.list.length !== 0}>
                            <div className="cen">
                                <img alt="" src="assets/imgs/zanwuxuexineirong.png" />
                            </div>
                            <p>暂无相关荣誉</p>
                        </div>
                        <div className='box' hidden={!get('type')}>
                            {
                                this.state.list.map((item,index)=>{
                                    return <div className='item' onClick={()=>{this.gotoPage('/honorinfo?id='+item.id)}}>
                                                <div className='itop' style={{backgroundImage:"url('"+item.image+"')"}}></div>
                                                <div className='ibom'>
                                                    <h2>{item.title}</h2>
                                                    <div className='itext'>{item.summary}</div>
                                                </div>
                                            </div>
                                })
                            }
                        </div>

                        <div className='pbox' hidden={get('type')}>
                            {
                                this.state.list.map((item,index)=>{
                                    return <div className='card' onClick={()=>{this.props.history.push('/honorinfo?id='+item.id);}}>
                                                <div className='cimg' style={{backgroundImage:"url('"+item.image+"')"}}></div>
                                                <div className='ctext'>
                                                    <h2>{item.title}</h2>
                                                    <p>{item.summary}</p>
                                                    <div className='cdiv'>
                                                        <img alt="" className='icon' src="assets/img/shijain.png" />
                                                        <h4>{item.createtime}</h4>
                                                    </div>
                                                    
                                                </div>
                                            </div>
                                })
                            }
                        </div>

                        <div className='r_page'>
                            <Pagination type="primary" showSizeChanger={false} pageSize={12}  defaultCurrent={this.state.page} total={this.state.total} onChange={(page)=>{this.changePage(page)}} />
                        </div>
                    </div>
                </div>
                <Footer />
            </div>
        )
    }
}
